package com.geostar.constructionland.analyse.service.adjust;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.geostar.constructionland.analyse.agri.AdjustAgri;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResultDlMmj;
import com.geostar.constructionland.analyse.entity.alyresult.tzhdlgdfx.AlyResultDlfx;
import com.geostar.constructionland.analyse.mapper.sptalyresult.AlyResultDlmmjMapper;
import com.geostar.constructionland.analyse.service.analyseresult.SticDlfxTzhService;
import com.geostar.constructionland.analyse.service.analyseresult.impl.JsydFxDlfxServiceImpl;
import com.geostar.constructionland.common.entity.response.LandInfo;
import com.geostar.constructionland.common.entity.response.model.AdjustModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class DlMmAdjust implements IAdjust{
    @Autowired
    AlyResultDlmmjMapper dlmmjMapper;
    @Autowired
    SticDlfxTzhService sticDlfxTzhService;
    @Autowired
    JsydFxDlfxServiceImpl jsydFxDlfxService;
    /**小数省略位*/
    private int iScale;
    /**阈值*/
    private double adjustThreshold;
    /**平差单位*/
    private double adjustUnit;
    /**平差对象*/
    private AdjustModel adjustModel;

    @Override
    public List<LandInfo> adjust(AdjustModel adjustModel) throws Exception {
        this.iScale = adjustModel.getAdjustAgrVersion().getIScale();
        this.adjustThreshold = adjustModel.getAdjustAgrVersion().getAdjustThreshold();
        this.adjustUnit = adjustModel.getAdjustAgrVersion().getAdjustUnit();
        this.adjustModel = adjustModel;
        List<LandInfo> returnLands = new ArrayList<>();
        List<AlyResultDlfx> alyResultDlfxes = jsydFxDlfxService.getMmjjsDlfxs(adjustModel.getFxbs());
        /**对每一个地块进行平差*/
        for(AlyResultDlfx resultDlfx : alyResultDlfxes){
            /**查找毛面积分析返回地块*/
            List<AlyResultDlMmj> dlMmjs =
                    dlmmjMapper.selectList(new QueryWrapper<AlyResultDlMmj>().eq("dkbs",resultDlfx.getDlid()));
            BigDecimal mmj_ys = dlMmjs.stream().map( dlMmj -> new BigDecimal(dlMmj.getTbmjYs())).reduce(BigDecimal.ZERO,BigDecimal::add);
            if(Math.abs(mmj_ys.subtract(new BigDecimal(resultDlfx.getDlmj())).doubleValue()) > adjustThreshold ){
                throw new Exception("地类毛面积平差值超出阈值," +
                        "毛面积分析图斑面积之和:{"+mmj_ys.doubleValue()+"},地类分析图版面积:{"+resultDlfx.getDlmj()+"},阈值:{"+adjustThreshold+"}");
            }
            /*计算和值*/
            BigDecimal mmj = dlMmjs.stream().map( dlMmj -> new BigDecimal(dlMmj.getTbmj()).setScale(iScale,BigDecimal.ROUND_HALF_UP))
                    .reduce(BigDecimal.ZERO,BigDecimal::add);
            BigDecimal mmj_total = new BigDecimal(resultDlfx.getTbdlmj()).setScale(iScale,BigDecimal.ROUND_HALF_UP);
            double adjustValue = mmj.subtract(mmj_total).doubleValue();
            /*平差排序*/
            if(adjustValue > 0){
                dlMmjs = dlMmjs.stream().sorted(Comparator.comparing(AlyResultDlMmj::getTbmj)).collect(Collectors.toList());
             }else{
                dlMmjs = dlMmjs.stream().sorted(Comparator.comparing(AlyResultDlMmj::getTbmj).reversed()).collect(Collectors.toList());
            }
            List<LandInfo> landInfos = dlMmjs.stream().map(dlMmj -> {
                LandInfo landInfo = new LandInfo();
                landInfo.setFxId(dlMmj.getFxid());
                landInfo.setZymj_ys(dlMmj.getTbmj());
                landInfo.setZymj_adjusted(dlMmj.getTbmj());
                return landInfo;
            }).collect(Collectors.toList());

            List<LandInfo> infosAdjusted = AdjustAgri.adjust(landInfos,adjustValue,adjustUnit);
            /*保存数据*/
            dlmmjMapper.updateTbmjAdjusted(infosAdjusted);
            returnLands.addAll(infosAdjusted);
        }
        return returnLands;
    }
}
